package com.jinbo.footmessage.serverInf;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import com.aigestudio.wheelpicker.core.AbstractWheelPicker;
import com.aigestudio.wheelpicker.view.WheelCurvedPicker;
import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.OrderBean;
import com.jinbo.footmessage.httpbean.ProjectBean;
import com.jinbo.footmessage.httpbean.WorkerInformationBean;
import com.jinbo.footmessage.utils.TimeUtils;
import com.jinbo.footmessage.view.TitleBarView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class PickerTimeActivity extends BaseActivity implements View.OnClickListener {
    private static String PROJECTS_INTENT="projects";
    public static void launch(Activity activity,WorkerInformationBean bean) {
        Intent intent = new Intent(activity, PickerTimeActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(PROJECTS_INTENT,bean);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    DatePicker datePicker;
    WheelCurvedPicker wheelCurvedPicker;
    TextView mTimeTV,mDateTv,mWeekDayTv,mBeginTimeTv,mEndTimeTv;

    Button mSubmitBt;

    private WorkerInformationBean mWorkerBean;
    private List<ProjectBean> mProjectsList;

    int year,month,day;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picker_time);
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("预约时间");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
        mTimeTV=(TextView)findViewById(R.id.time);
        datePicker=(DatePicker)findViewById(R.id.date_picker);
        mDateTv=(TextView)findViewById(R.id.date);
        mWeekDayTv=(TextView)findViewById(R.id.day_of_week);
        mBeginTimeTv=(TextView)findViewById(R.id.begin_time);
        mEndTimeTv=(TextView)findViewById(R.id.end_time);
        mSubmitBt=(Button)findViewById(R.id.submit);
        mSubmitBt.setOnClickListener(this);
        mWorkerBean=(WorkerInformationBean)getIntent().getSerializableExtra(PROJECTS_INTENT);
        mProjectsList=mWorkerBean.projects;
        final int time=getTime(mProjectsList);
        mTimeTV.setText(time+"");

        final Calendar calendar=Calendar.getInstance();
         year=calendar.get(Calendar.YEAR);
         month=calendar.get(Calendar.MONTH)+1;
         day=calendar.get(Calendar.DAY_OF_MONTH);
        mDateTv.setText(TimeUtils.getDate(year + "-" + month+ "-" + day));

        datePicker.init(year, month-1, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                PickerTimeActivity.this.year=year;
                month=monthOfYear+1;
                day=dayOfMonth;
                mDateTv.setText(TimeUtils.getDate(year + "-" + (monthOfYear+1) + "-" + dayOfMonth));
                calendar.set(year, monthOfYear, dayOfMonth);
                String weekDay = TimeUtils.getWeek(calendar);
                mWeekDayTv.setText(weekDay);
            }
        });

        wheelCurvedPicker = (WheelCurvedPicker) findViewById(R.id.wheel_picker);
        List<String> data = new ArrayList<>();
        data.add("09:00开始");
        data.add("11:00开始");
        data.add("13:00开始");
        data.add("15:00开始");
        data.add("17:00开始");
        wheelCurvedPicker.setData(data);
        wheelCurvedPicker.setItemCount(3);
        wheelCurvedPicker.setItemSpace(50);
        wheelCurvedPicker.setOnWheelChangeListener(new AbstractWheelPicker.OnWheelChangeListener(){
            @Override
            public void onWheelScrolling(float deltaX, float deltaY) {

            }

            @Override
            public void onWheelSelected(int index, String data) {
                String [] strs=data.split("开始");
                mBeginTimeTv.setText(strs[0]);
                mEndTimeTv.setText(TimeUtils.getEndTime(strs[0],time));
            }

            @Override
            public void onWheelScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
            case R.id.submit:
                OrderBean orderBean=new OrderBean();
                orderBean.worker=mWorkerBean;
                orderBean.date=mDateTv.getText().toString();
                orderBean.week=mWeekDayTv.getText().toString();
                orderBean.begin_time=mBeginTimeTv.getText().toString();
                orderBean.end_time=mEndTimeTv.getText().toString();
                orderBean.content=getProjectNames(mProjectsList);
                orderBean.total_price=getPrice(mProjectsList)+"";
                ConfirmOrderActivity.launch(this,orderBean);
                break;
        }
    }
    private int getPrice(List<ProjectBean> mProjectsList) {
        int sum = 0;
        for (int i = 0; i < mProjectsList.size(); i++)
            if (mProjectsList.get(i).flag)
                sum = sum + Integer.valueOf(mProjectsList.get(i).price);
        return sum;
    }
    String getProjectNames(List<ProjectBean> projects) {
        String s = "";
        for (int i = 0; i < projects.size(); i++) {
            if (projects.get(i).flag)
                s = s + projects.get(i).name + "/";
        }
        if (!s.equals("")) {
            int length = s.length();
            return s.substring(0, length - 1);
        } else
            return s;
    }

    private  int getTime(List<ProjectBean> mProjectsList)
    {
        int sum=0;
        for(int i=0;i<mProjectsList.size();i++)
            if (mProjectsList.get(i).flag)
                sum=sum+Integer.valueOf(mProjectsList.get(i).duration);
        return sum;
    }
}
